<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=8">

    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>用户管理</title>
    <link rel="stylesheet" th:href="@{/layui/layui/css/layui.css}">
   
</head>
<body>
<div class="layui-main">
    <div class="demoTable" style="margin-top: 15px;">
        查询用户：
        <div class="layui-inline">
            <input class="layui-input" name="id" id="demoReload" placeholder="请输入用户名或邮箱" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload" onclick="query()">查询</button>
    </div>
    <br />
    <table id="dates" lay-filter="test"></table>
</div>

<!--        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>-->
<!--        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->



<script th:src="@{/static/js/jquery-3.4.1.min.js}" type="text/javascript"></script>
<script th:src="@{/layui/layui/layui.js}"></script>

    <script type="text/javascript">
        var tableIns = null;
        /**
         * 禁用 ajax 缓存
         */
        $.ajaxSetup({
            cache: false
        });
        var table = null;
        layui.use(['form','table','layer'], function(){
            table = layui.table;
            var layer = layui.layer;
            var form = layui.form;
            form.on('checkbox', function (data) {
                if (this.checked == true) {
                    var id = $(this).val();
                    setisko(id,1);
                } else {
                    var id = $(this).val();
                    setisko(id,-1);
                }
            });

            tableIns = table.render({
                elem: '#dates'
                ,cache: false//关闭本地磁盘缓存，
                ,defaultToolbar:['filter', 'print']
                ,url:'/admin/selectusertable2'
                //,limit:20 //默认为10
                ,cols: [ [
                    {field:'username',  title: '用户名'}
                    ,{field:'email',width:170,  title: '邮箱'}
                    ,{field:'password',width:170,  title: '密码'}
                    ,{field:'birthder',width:120,  title: '注册时间',sort: true}
                    ,{field:'level',width:100,  title: '等级',templet: function(d){
                            var str = '';
                            if(d.level==1){str='普通用户';}
                            else if(d.level==2){str='管理员';}
                            return str;
                        }
                    }
                    ,{field:'memory',width:120,  title: '可用容量',templet: function(d){
                            var str = '';
                            if(d.memory==-1){str='无限制';}
                            else{str=d.memory;}
                            return str+" M";
                        }
                    }
                    ,{field:'level',width:120,  title: '状态',templet: function(d){
                            var str = '';
                            if(d.isok==1){str = '  <form class="layui-form" action=""><div class="layui-form-item">\n' +
                                '      <input type="checkbox" checked="checked"  value="'+d.id+'" lay-filter="keyong" name="like[write]" title="可用">\n' +
                                '  </div></form>';}
                            else {str = '  <form class="layui-form" action=""><div class="layui-form-item">\n' +
                                '      <input type="checkbox" value="'+d.id+'"  lay-filter="jinyong" name="like[write]" title="禁用">\n' +
                                '  </div></form>';}
                            return str;
                        }
                    }
                    ,{field:'level',width:110,  title: '操作',templet: function(d){
                            //return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';
                            return '<div class="layui-btn-group">\n' +
                                '  <button type="button" class="layui-btn layui-btn-sm" lay-event="memory">\n' +
                                '    <i class="layui-icon">&#xe642;</i>\n' +
                                '  </button>\n' +
                                '  <button type="button" class="layui-btn layui-btn-sm"  lay-event="del">\n' +
                                '    <i class="layui-icon">&#xe640;</i>\n' +
                                '  </button>\n' +
                                '</div>';
                        }
                    }
                ] ]
                ,username:'sousuo'
                ,page: true
                ,done:function(res, curr, count){
                    //数据的回调用，可不写
                }
            });

            // 行点击事件
            table.on('tool', function (obj) {
                var data = obj.data;
                var event = obj.event;
                if (event === 'memory') {
                    layer.open({
                        type: 2,
                        title:'用户容量',
                        shadeClose: true,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['420px', '300px'], //宽高
                        // content: '<div style="width: 360px;"><br /><br /><div class="layui-form-item">\n' +
                        //     '  <label class="layui-form-label">用户容量(M)</label>\n' +
                        //     '  <div class="layui-input-block">\n' +
                        //     '  <input type="text" name="title" id="tests" value="'+data.memory+'" required lay-verify="required" placeholder="请输入正整数" autocomplete="off" class="layui-input">  ' +
                        //         '<span style="color: #DAA5A6;">输入“-1”则为无限制,请输入整数</span><br/><br/><button type="button" class="layui-btn layui-btn-radius layui-btn-normal" onclick="rongliang('+data.id+')">保 存</button>'+
                        //     '  </div>\n' +
                        //     '</div></div>'
                        content: '/admin/root/modifyuser?uid='+data.uid+'&id='+data.id
                        ,end:function(){
                            table.reload('dates');//重新获取表数据
                        }
                    });
                }else if (event === 'del') {
                    var index = layer.confirm('确定删除此用户？', {
                        btn: ['删除','取消'] //按钮
                    }, function(){
                        $.ajax({
                            type: "POST",
                            url: "/admin/root/deleuser",
                            data: {id: data.id},
                            dataType: "json",
                            success: function (data) {
                                if (data > 0) {
                                    table.reload('dates');//重新获取表数据
                                    layer.msg('删除成功', {icon: 1});
                                }else if(data==-1){
                                    layer.msg('不能删除自己', {icon: 2});
                                }
                            }
                        });
                        layer.close(index);
                    }, function(){

                    });
                }else{
                    layer.msg('参数异常', {icon: 2});
                }
            });
        });
function setisko(id,val) {
    $.ajax({
        type: "POST",
        url: "/admin/root/setisok",
        data: {id: id,isok:val},
        dataType: "json",
        success: function (data) {
            if (data > 0) {
                table.reload('dates');//重新获取表数据
                layer.msg('设置成功', {icon: 1});
            }else if(data==-1){
                layer.msg('设置失败', {icon: 2});
            }
        }
    });
}
function rongliang(id) {
    var memory = $('#tests').val();
    $.ajax({
        type: "POST",
        url: "/admin/root/setmemory",
        data: {id: id,memory:memory},
        dataType: "json",
        success: function (data) {
            if (data > 0) {
                table.reload('dates');//重新获取表数据
                layer.msg('保存成功', {icon: 1});
            }else{
                layer.msg('保存失败', {icon: 2});
            }
        }
    });
}

        function query(){
    var data = $('#demoReload').val();
            //这里以搜索为例
            tableIns.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    username: data
                    //,bbb: 'yyy'
                    //…
                }
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }
    </script>
</body>
</html>